
<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getUnconfirmedAlarmApi, confirmedAlarmApi, getSearchAralmApi } from '@/api/alarm'
import { getCoalYardTemperatureApi } from '@/api/infrared'
import { any } from 'video.js/dist/types/utils/events'
import { formatTime } from '@/utils/common'

interface AlarmData {
 recoverTime: '',
  position: ''
}

// 定义变量

const temperData = reactive({
  yard1: {
    max: '',
    avg: ''
  },
  yard2: {
    max: '',
    avg: ''
  }
})

let maxTemperature: any = ref(39)
let avgTemperature: any = ref(41)

const alarmData = reactive({
  co: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  ch4: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  smoke: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  dust: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  o2: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  infrared: [ { recoverTime: '', position: '', creationTime: '', content: '' } ],
  saveCoal: [ { recoverTime: '', position: '', creationTime: '', content: '' } ]
})

const activeName = ref('')
// 报警详情弹框的控制变量
const AlarmDetailDialogVisible = ref(false)
// 报警弹框数据
const dailogData = reactive({
  baryardName: null,
  batchno: null,
  content: null,
  creationTime: null,
  id: null,
  isWarn: 0,
  lastModificationTime: null,
  level: null,
  name: null,
  operator: null,
  operatorDescription: null,
  position: null,
  recoverTime: null,
  sectionCode: null,
  sectionName: null,
  status: null,
  type: '',
  yardName: ''
})
// 定时器
let interval:any

// 定义方法
onMounted(async () => {
  getUnconfirmedAlarm()
  // getCoalYardTemperature()
  // getFlameCoalMonitor()
  // interval = setInterval(() => {
  //   getUnconfirmedAlarm()
  //   getCoalYardTemperature()
  //   // getFlameCoalMonitor()
  // }, 5000)
})

// 获取煤场温度信息
const getCoalYardTemperature = async () => {
  maxTemperature.value = 0
  avgTemperature.value = 0
  let res:any = await getCoalYardTemperatureApi()
  res.result.forEach((item:any) => {
    maxTemperature.value += item.maxTemper
    avgTemperature.value += item.avgTemper
  })
  // if (res.result.infHisList.length) {
  //   temperData.yard1.max = res.result.infHisList[0].maxTemper
  //   temperData.yard1.avg = res.result.infHisList[0].avgTemper
  //   temperData.yard2.max = res.result.infHisList[5].maxTemper
  //   temperData.yard2.avg = res.result.infHisList[5].avgTemper
  // } else {
  //   temperData.yard1.max = '0'
  //   temperData.yard1.avg = '0'
  //   temperData.yard2.max = '0'
  //   temperData.yard2.avg = '0'
  // }
  console.log('煤场温度信息------->', temperData, res)
} 

// 获取明火煤监测信息
const getFlameCoalMonitor = async () => {
  // let res = await 
  // console.log('明火煤监测信息------->')
  
} 

// 获取待确认报警数量信息
const getUnconfirmedAlarm = async () => {
  let res = await getUnconfirmedAlarmApi({ SkipCount: 0, MaxResultCount: 999 })
  console.log('待确认报警数量信息------->', res)
  alarmData.ch4 = []
  if (res.totalCount) {
    res.items.forEach((item:any) => {
      switch (item.type) {
      case '202':
        alarmData.co = item.searchWarnRecordHistoryDtos
        break
      case 'CH4':
        alarmData.ch4.push(item)
        break
      case '204':
        alarmData.smoke = item.searchWarnRecordHistoryDtos
        break
      case '205':
        alarmData.dust = item.searchWarnRecordHistoryDtos
        break
      case '208':
        alarmData.o2 = item.searchWarnRecordHistoryDtos
        break
      case 'Infrared':
        alarmData.infrared.push(item)
        break
      case '存煤':
        alarmData.saveCoal.push(item)
        break
      }
      
    })
  }
}
// 获取未确认报警详情数据
const getUnconfirmedAlarmDetail = async (type:string) => {
  
  let data = {
    type: type,
    status: ''
  }
  let res = await getSearchAralmApi(data)
  console.log('展开折叠栏------>', data, res)
}

// 展示报警详情
const showAlarmDetail = (item: any) => {
  console.log('选择的报警信息---->', item)
  dailogData.baryardName = item.baryardName
  dailogData.batchno = item.batchno
  dailogData.content = item.content
  dailogData.creationTime = item.creationTime
  dailogData.id = item.id
  dailogData.isWarn = item.isWarn
  dailogData.lastModificationTime = item.lastModificationTime
  dailogData.level = item.level
  dailogData.name = item.name
  dailogData.operator = item.operator
  dailogData.operatorDescription = item.operatorDescription
  dailogData.position = item.position
  dailogData.recoverTime = item.recoverTime
  dailogData.sectionCode = item.sectionCode
  dailogData.sectionName = item.sectionName
  dailogData.status = item.status
  dailogData.yardName = item.yardName
  switch (item.type) {
  case 'CO':
    dailogData.type = '一氧化碳'
    break
  case 'CH4':
    dailogData.type = '甲烷'
    break
  case '204':
    dailogData.type = '烟感'
    break
  case 'FOC':
    dailogData.type = '粉尘'
    break
  case '208':
    dailogData.type = '氧气'
    break
  case '209':
    dailogData.type = '红外'
    break
  }
  
  AlarmDetailDialogVisible.value = true
}

// 确认报警
const confirmAlarm = async () => {
  console.log('确认时候的id------>', dailogData.id)
  let data = {
    id: dailogData.id ? dailogData.id : '0',
    operatorDescription: dailogData.operatorDescription ? dailogData.operatorDescription : 'text'
  }
  // let res = await confirmedAlarmApi(data)
  let res = await confirmedAlarmApi(data.id)
  if (res.code === -1) {
    ElMessage({
      message: '确认失败！！！',
      type: 'warning'
    })
  } else {
    ElMessage({
      message: '确认成功！！！',
      type: 'success'
    })
    getUnconfirmedAlarm()
    AlarmDetailDialogVisible.value = false
  }
  console.log('确认报警---->', dailogData, res)
}

// 销毁页面
onUnmounted(() => {
  clearInterval(interval)
})

// 向外暴露的函数
defineExpose({
  getUnconfirmedAlarm,
  getCoalYardTemperature
})
</script>
<template>
  <div class="left-main-box">
    <div class="coal-yard-box">
      <div class="coal-yard-name">
        一煤场
      </div>
      <div class="show-info-box">
        <div>
          <div class="title-box">
            平均温度
          </div>
          <div
            :id=" false ? '' : (false ? 'blue' : 'red')"
            class="temperature-box"
          >
            {{ avgTemperature }}℃
          </div>
        </div>
        <div>
          <div class="title-box">
            最高温度
          </div>
          <div
            :id=" true ? '' : (true ? 'blue' : 'red')"
            class="temperature-box"
          >
            {{ maxTemperature }}℃
          </div>
        </div>
      </div>
      <div class="coal-yard-name">
        二煤场
      </div>
      <div class="show-info-box">
        <div>
          <div class="title-box">
            平均温度
          </div>
          <div
            :id=" false ? '' : (true ? 'blue' : 'red')"
            class="temperature-box"
          >
            {{ avgTemperature }}℃
          </div>
        </div>
        <div>
          <div class="title-box">
            最高温度
          </div>
          <div
            :id=" true ? '' : (true ? 'blue' : 'red')"
            class="temperature-box"
          >
            {{ maxTemperature }}℃
          </div>
        </div>
      </div>
    </div>
    <!-- <div class="monitor-box">
      <div class="title-box">
        明火煤监测
      </div>
      <div class="info-box">
        <div>
          1号卸煤沟
        </div>
        <div
          :id="true ? '' : 'monitor-red'"
          class="icon-box"
        />
      </div>
      <div class="info-box">
        <div>
          2号卸煤沟
        </div>
        <div
          :id="true ? '' : 'monitor-red'"
          class="icon-box"
        />
      </div>
      <div class="info-box">
        <div>
          出煤口
        </div>
        <div
          :id="false ? '' : 'monitor-red'"
          class="icon-box"
        />
      </div>
    </div> -->
    <div class="collapse-box">
      <el-collapse
        v-model="activeName"
        class="collapse-item"
        accordion
        @change="getUnconfirmedAlarmDetail"
      >
        <el-collapse-item
          name="203"
          :disabled="!alarmData.ch4.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-jiawan" /><span>甲烷</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.ch4.length"
                class="alarm-num"
              >
                {{ alarmData.ch4.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.ch4"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ formatTime(item.creationTime) }}
            </div>
            <div class="alarm-location">
              {{ item.content }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="202"
          :disabled="!alarmData.co.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-yiyanghuatan" /><span>一氧化碳</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.co.length"
                class="alarm-num"
              >
                {{ alarmData.co.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.co"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.creationTime }}
            </div>
            <div class="alarm-location">
              {{ item.content }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="208"
          :disabled="!alarmData.o2.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-yangqi" /><span>氧气</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.o2.length"
                class="alarm-num"
              >
                {{ alarmData.o2.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.o2"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.recoverTime }}
            </div>
            <div class="alarm-location">
              {{ item.position }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="204"
          :disabled="!alarmData.smoke.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-yangan" /><span>烟感</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.smoke.length"
                class="alarm-num"
              >
                {{ alarmData.smoke.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.smoke"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.recoverTime }}
            </div>
            <div class="alarm-location">
              {{ item.position }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="205"
          :disabled="!alarmData.dust.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-fenchen" /><span>粉尘</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.dust.length"
                class="alarm-num"
              >
                {{ alarmData.dust.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.dust"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.recoverTime }}
            </div>
            <div class="alarm-location">
              {{ item.position }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="209"
          :disabled="!alarmData.infrared.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-hongwaichuanganqi" /><span>红外</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.infrared.length"
                class="alarm-num"
              >
                {{ alarmData.infrared.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item,index) in alarmData.infrared"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.recoverTime }}
            </div>
            <div class="alarm-location">
              {{ item.position }}报警！！！
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item
          name="210"
          :disabled="!alarmData.saveCoal.length"
        >
          <template #title>
            <div class="collapse-title">
              <div class="iconfont icon-hongwaichuanganqi" /><span>存煤</span><el-icon class="header-icon" />
              <div
                v-show="alarmData.saveCoal.length"
                class="alarm-num"
              >
                {{ alarmData.saveCoal.length }}
              </div>
            </div>
          </template>
          <div
            v-for="(item, index) in alarmData.saveCoal"
            :key="index"
            class="monitor-info-item"
            @click="showAlarmDetail(item)"
          >
            <div class="alarm-time">
              {{ item.recoverTime }}
            </div>
            <div class="alarm-location">
              {{ item.position }}报警！！！
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <el-dialog
      v-model="AlarmDetailDialogVisible"
      class="alarm-details"
    >
      <template #header>
        <div class="alarm-header-box">
          <div class="title">
            报警详情
          </div>
        </div>
      </template>
      <div class="info-box">
        <div class="left-box">
          <div class="alarm-type">
            报警类型
          </div>
          <div class="launch-info">
            投放信息
          </div>
          <div class="alarm-time">
            报警时间
          </div>
          <div class="device-location">
            设备位置
          </div>
        </div>
        <div class="right-box">
          <div class="type-info">
            {{ dailogData.type }}
          </div>
          <div class="launch-detail">
            {{ dailogData.content }}
          </div>
          <div class="time-info">
            {{ formatTime(dailogData.creationTime ? dailogData.creationTime:'') }}
          </div>
          <div class="location-info">
            {{ dailogData.yardName }}
          </div>
        </div>
      </div>
      <div
        class="confirm-btn"
        @click="confirmAlarm"
      />
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.left-main-box {
  width: 100%;
  height: 100%;
  margin-top: 1%;

  /* border: 1px solid green; */
  .coal-yard-box {
    width: 100%;
    height: 25%;
    background: linear-gradient(90deg, #fff 0%, rgb(255 255 255 / 30%) 100%);
    border: 1px solid #fff;
    border-radius: 6px;
    box-shadow: 0 2px 10px 0 rgb(166 184 208 / 40%);
    opacity: 1;
    .coal-yard-name {
      height: 2.04vh;
      margin-top: 1vh;
      margin-left: 2vh;
      overflow: hidden;
      color: #4e5969;
      font-size: 16px;
      font-family: PingFang,sans-serif;
      line-height: 2.04vh;
      background-clip: text;
    }
    .show-info-box {
      display: flex;

      /* margin-top: 6px; */
      margin-top: 0.6vh;
      > div {
        /* width: 100px;
        height: 68px;
        margin-left: 5px; */
        width: 5.21vw;
        height: 6.9vh;
        margin-left: 0.5vh;
        background: #dfeaf7;
        border-radius: 6px;
        opacity: 0.5;
        .title-box {
          /* width: 56px;
          height: 20px;
          margin-top: 10px;
          margin-left: 16px; */

          /* width: 2.92vw; */
          height: 1.85vh;
          margin: 1vh;
          margin-left: 0.83vw;
          overflow: hidden;
          color: #6b7785;
          font-weight: 400;
          font-size: 14px;
          font-family: PingFang,sans-serif;
          line-height: 1.85vh;
          background-clip: text;
        }
        .temperature-box {
          /* width: 39px;
          height: 23px;
          margin-left: 16px; */

          /* width: 2.03vw; */
          height: 2.13vh;
          margin-left: 0.83vw;
          overflow: hidden;
          color: #272e3bff;
          font-weight: bold;
          font-size: 20px;

          /* font-family: "DIN Alternate-Bold", "DIN Alternate"; */
          line-height: 2.13vh;
          background-clip: text;
        }
      }
    }
  }
  .monitor-box {
    /* width: 220px;
    height: 150px; */
    width: 100%;
    height: 15.89vh;
    margin-top: 1vh;
    background: rgb(255 255 255 / 60%);
    border: 1px solid #fff;
    border-radius: 6px;
    box-shadow: 0 2px 10px 0 rgb(166 184 208 / 40%);
    opacity: 1;
    .title-box {
      /* width: 80px;
      height: 22px;
      margin-top: 8px;
      margin-left: 16px; */

      /* width: 4.17vw; */
      height: 2.3vh;
      margin-top: 0.8vh;
      margin-left: 0.8vw;
      overflow: hidden;
      color: #4e5969;
      font-weight: 600;
      font-size: 16px;
      font-family: PingFang,sans-serif;
      line-height: 2.3vh;
      background-clip: text;
    }
    .info-box {
      display: flex;
      justify-content: space-between;

      /* width: 194px;
      height: 32px;
      margin-top: 4px;
      margin-left: 8px;
      line-height: 32px; */

      /* width: 10.1vw; */
      height: 3.5vh;
      margin-top: 0.4vh;
      margin-left: 0.4vw;
      overflow: hidden;
      line-height: 3.5vh;
      background: #dfeaf7;
      border-radius: 6px;
      opacity: 0.5;
      > div {
        /* width: 72px;
        height: 20px;
        margin-left: 10px; */
        width: 3.75vw;
        height: 2vh;
        margin-left: 0.5vw;
        color: #6b7785;
        font-weight: 500;
        font-size: 14px;
        font-family: PingFang,sans-serif;
        background-clip: text;
      }
      .icon-box {
        width: 16px;
        height: 16px;

        /* margin-top: 6px;
        margin-right: 10px; */
        margin-top: 0.6vh;
        margin-right: 0.5vw;
        background: linear-gradient(180deg, #26ffbe 0%, #029b80 100%);
        border: 2px solid #fff;
        border-radius: 16px;
        box-shadow: 0 2px 4px 0 rgb(34 198 178 / 40%);
        opacity: 1;
      }
    }
  }
  .collapse-box {
    /* width: 220px;
    margin-top: 10px;
    margin-left: 10px; */
    width: 100%;
    margin-top: 1vh;

    /* border: 1px solid red; */
    .collapse-item {
      width: 100%;

      /* margin-top: 10px; */
      margin-top: 1vh;

      /* border: 1px solid red; */
      .collapse-title {
        display: flex;
        align-items: center;
        justify-content: space-around;
        width: 100%;

        /* height: 31px;
        line-height: 32px; */
        height: 3.5vh;
        > span {
          width: 50%;

          /* width: 100px; */

          /* border: 1px solid green; */
        }
        .iconfont {
          top: 6px;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 20px;
          height: 20px;
          background: linear-gradient(180deg, #54aafe 0%, #4482ed 100%);

          /* border: 1px solid blue; */
          border-radius: 4px;
          box-shadow: 0 2px 4px 0 #bbd2ff;
        }
        .alarm-num {
          /* margin-top: 6px;
          margin-right: 10px;
          margin-left: 15px; */
          display: flex;
          align-items: center;
          justify-content: center;
          width: 16px;
          height: 16px;
          color: #fff;
          background: linear-gradient(180deg, #e21212 0%, #d65a5a 100%);
          border: 2px solid #fff;
          border-radius: 16px;
          box-shadow: 0 2px 4px 0 rgb(34 198 178 / 40%);
          opacity: 1;
        }

        /* border: 1px solid red; */
      }
      .monitor-info-item {
        width: 200px;
        margin-bottom: 10px;
        margin-left: 12px;
        padding-bottom: 10px;
        border-bottom: 1px solid #e5e6eb;
        > div {
          width: 200px;
          margin: 6px 0;
          overflow: hidden;
          color: #86909c;
          font-weight: bold;
          font-size: 14px;

          /* font-family: DIN-Bold, DIN; */
          line-height: 16px;
          white-space: nowrap;
          text-overflow: ellipsis;
          background-clip: text;
        }
      }
      .monitor-info-item:hover {
        cursor: pointer;
      }
    }
  }
  .alarm-header-box {
    .title {
      width: 120px;
      height: 28px;
      padding-left: 15px;
      color: #4e5969;
      font-weight: 600;
      font-size: 20px;
      font-family: PingFang,sans-serif;
      line-height: 23px;
      background-clip: text;
    }
  }
  .info-box {
    display: flex;
    padding-left: 15px;

    /* border: 1px solid red; */
    .left-box {
      width: 64px;

      /* border: 1px solid blue; */
      > div {
        width: 64px;
        height: 22px;
        margin-bottom: 20px;
        color: #86909c;
        font-weight: 400;
        font-size: 16px;
        font-family: PingFang,sans-serif;
        line-height: 22px;
        background-clip: text;
      }
      .launch-info {
        height: 60px;
        line-height: 26px;
      }
    }
    .right-box {
      width: 556px;
      margin-left: 20px;

      /* border: 1px solid blue; */
      > div {
        width: 556px;
        height: 22px;
        margin-bottom: 20px;
        color: #4e5969;
        font-size: 16px;
        font-family: PingFang,sans-serif;
        line-height: 22px;
        background-clip: text;
      }
      .launch-detail {
        height: 60px;
        line-height: 26px;
      }
    }
  }
  .confirm-btn {
    width: 120px;
    height: 42px;
    margin-top: 25px;
    margin-left: 280px;
    background-image: url("../assets//bckImg/confirm.png");
  }
  .confirm-btn:hover {
    cursor: pointer;
  }
  .confirm-btn:active {
    background: rgba($color: #2191ffff, $alpha: 90%);
    cursor: pointer;
  }
  .icon-hongwai2 {
    transform: rotate(180deg);
  }
}
</style>
<style>
.el-collapse-item {
  margin-top: 7px;
}
.el-collapse-item__wrap {
  max-height: 200px;
  overflow: auto;
  border: 1px solid #fff;
  border-radius: 6px;
  box-shadow: 0 2px 10px 0 rgb(166 184 208 / 40%);
}
.el-collapse-item__wrap::-webkit-scrollbar {
  display: none;
}
.el-collapse-item__header {
  height: 32px;
  border: 1px solid #fff;
  border-radius: 6px;
  box-shadow: 0 2px 10px 0 rgb(166 184 208 / 40%);
  opacity: 1;
}
.alarm-details {
  width: 700px;
  height: 390px;
}
</style>